package
{
   import flash.events.Event;
   import flash.events.HTMLUncaughtScriptExceptionEvent;
   import flash.geom.Point;
   import flash.geom.Rectangle;
   import flash.html.HTMLHost;
   import flash.html.HTMLLoader;
   import flash.html.HTMLWindowCreateOptions;
   import flash.net.URLRequest;
   import flash.net.navigateToURL;
   
   public class MyHost extends HTMLHost
   {
       
      
      private var html:HTMLLoader;
      
      private var _title:String;
      
      private var noMainWin:Boolean;
      
      public function MyHost(defaultBehavior:Boolean = true, isNoMainWindow:Boolean = false)
      {
         super(defaultBehavior);
         this.noMainWin = isNoMainWindow;
      }
      
      override public function createWindow(param1:HTMLWindowCreateOptions) : HTMLLoader
      {
         var rect:Rectangle = new Rectangle(0,0,790,525);
         var p:Point = this.htmlLoader.stage.nativeWindow.globalToScreen(new Point(470 - rect.width / 2,295 - rect.height / 2));
         rect.x = p.x;
         rect.y = p.y;
         html = HTMLLoader.createRootWindow(false,null,false,rect);
         html.stage.nativeWindow.alwaysInFront = true;
         html.htmlHost = new MyHost(true,true);
         html.addEventListener("uncaughtScriptException",this.onErr);
         return html;
      }
      
      protected function onErr(param1:HTMLUncaughtScriptExceptionEvent) : void
      {
         var _loc2_:String = null;
         try
         {
            _loc2_ = "";
            for(var _loc3_:* in param1.exceptionValue)
            {
               _loc2_ += _loc3_ + "=" + param1.exceptionValue[_loc3_] + ";";
            }
            Log.WriteLog(_loc2_ + "\n");
         }
         catch(error:Error)
         {
            trace("error" + error);
         }
      }
      
      override public function updateTitle(title:String) : void
      {
         if(!this._title)
         {
            this._title = title;
         }
         title = this._title;
         super.updateTitle(title);
      }
      
      override public function updateLocation(param1:String) : void
      {
         if(noMainWin && param1.indexOf("://") > -1 && this.htmlLoader && !OpenNewWindowModel.HasIn(htmlLoader.location))
         {
            htmlLoader.stage.nativeWindow.close();
            navigateToURL(new URLRequest(htmlLoader.location));
            return;
         }
         if(noMainWin && param1.indexOf("://") > -1 && this.htmlLoader && OpenNewWindowModel.HasIn(htmlLoader.location))
         {
            htmlLoader.stage.nativeWindow.activate();
            return;
         }
         super.updateLocation(param1);
      }
      
      protected function onComplete(param1:Event) : void
      {
         this.html.removeEventListener(Event.COMPLETE, this.onComplete);
         if(!html.stage.nativeWindow.closed)
         {
            html.stage.nativeWindow.activate();
         }
      }
   }
}
